package com.cqc.recyclerviewheaderfooter01;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Context context = this;
    private RecyclerView recyclerView;
    private MyAdapter adapter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);

        View headView = View.inflate(context, R.layout.layout_header, null);
        View footView = View.inflate(context, R.layout.layout_footer, null);
//        View headView = LayoutInflater.from(context).inflate(R.layout.layout_header,recyclerView, false);
//        View footView =LayoutInflater.from(context).inflate(R.layout.layout_footer, recyclerView,false);

        adapter.setHeaderView(headView);
        adapter.setFooterView(footView);
    }

    public class MyAdapter extends RecyclerView.Adapter {

        //默认只加一个header 和footer,2种类型不能为0，因为getItemViewType(int position)默认返回0
        private int HeaderType = 1;
        private int FooterType = 2;

        private View headerView;
        private View footerView;
        public MyViewHolder holder;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == HeaderType) {
                return new MyViewHolder(headerView);
            }
            if (viewType == FooterType) {
                return new MyViewHolder(footerView);
            }
//            View itemView = View.inflate(context, R.layout.item, null);
            View itemView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            if (getItemViewType(position) == HeaderType) {
                return;
            }
            if (getItemViewType(position) == FooterType) {
                return;
            }

            holder = (MyViewHolder) viewHolder;
            holder.tv_item.setText("tv_item" + position);
        }

        @Override
        public int getItemCount() {
            return 100 + getHeadViewCount() + getFootViewCount();
        }


        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return HeaderType;
            }
            if (position == 100 + getHeadViewCount()) {
                return FooterType;
            }
            return super.getItemViewType(position);
        }

        public void setHeaderView(View headerView) {
            this.headerView = headerView;
        }

        public void setFooterView(View footerView) {
            this.footerView = footerView;
        }

        public int getHeadViewCount() {
            return headerView == null ? 0 : 1;
        }

        public int getFootViewCount() {
            return footerView == null ? 0 : 1;
        }

        public class MyViewHolder extends RecyclerView.ViewHolder {

            TextView tv_item;

            public MyViewHolder(View itemView) {
                super(itemView);
                if (itemView == headerView) {
                    return;
                }
                if (itemView == footerView) {
                    return;
                }
                tv_item = (TextView) itemView.findViewById(R.id.tv_item);
            }
        }
    }
}
